<template>
  <div>
    <el-card class="box-1">
      <div class="header">
        <div>
          <el-button type="success" class="do-btn" icon="el-icon-search" @click="showMoneyChange">{{ title }}管理</el-button>
        </div>
      </div>
      <el-table v-loading="tableLoading" :data="tableData" :max-height="tableMaxHeight" border highlight-current-row @sort-change="handleSortChange">
        <el-table-column prop="id" label="ID" min-width="70" header-align="center" align="center" sortable />
        <el-table-column prop="money" label="支付金额" min-width="110" header-align="center" align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.money.indexOf('-')===0?'danger':'success'">{{ scope.row.money }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="wallet" label="余额" min-width="110" header-align="center" align="center" />
        <el-table-column prop="operation" label="操作记录" min-width="300" header-align="center"/>
        <el-table-column prop="remarks" label="备注" min-width="160" />
        <el-table-column prop="manage" label="操作人" min-width="120" />
        <el-table-column prop="created_at" label="操作时间" min-width="160" header-align="center" align="center" />
      </el-table>
      <el-pagination
        :current-page.sync="currentPage"
        :page-size="pageSize"
        :page-sizes="pageSizes"
        :total="total"
        class="pagination"
        layout="total, sizes, prev, pager, next, jumper"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"/>
    </el-card>
    <!--修改客户资金弹窗-->
    <el-dialog :visible.sync="moneyChangeDialogVisible" :title="title+'管理'" width="480px" class="dialog" center @close="resetMoneyChangeDialog('form')">
      <el-form ref="form" :model="form" label-width="90px">
        <el-form-item>
          <el-radio-group v-model="form.money_type">
            <el-radio :label="1">增加</el-radio>
            <el-radio :label="-1">减少</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="title==='赠送金'" label="类型">
          <el-select v-model="form.type">
            <el-option :value="1" label="充值赠送" />
            <el-option :value="2" label="活动补贴" />
            <el-option :value="-1" label="冲账" />
            <el-option :value="-2" label="罚款" />
            <el-option :value="3" label="其他" />
          </el-select>
        </el-form-item>
        <el-form-item :label="title" prop="label">
          <el-input v-model="form.money" />
        </el-form-item>
        <el-form-item label="备注" prop="description">
          <el-input v-model="form.remarks" type="textarea" rows="3"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="moneyChangeDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitMoneyChange('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { handleCurrentChange, handleSizeChange, handleSortChange } from '../../utils/table';
export default {
  data() {
    return {
      title: '',
      searchTerms: {
        searchValue: ''
      },

      id: null,
      moneyChangeDialogVisible: false,
      form: {
        money_type: 1,
        type: 1,
        money: 0,
        remarks: ''
      },

      // 表格相关数据
      tableMaxHeight: document.documentElement.clientHeight - 280,

      // 表格相关参数
      tableLoading: false,
      currentPage: 1,
      pageSize: this.$store.getters.userInfo.pageSize,
      pageSizes: [10, 20, 50, 100, 1000],
      total: 0,
      tableData: []
    }
  },
  created() {
    this.title = this.$route.query.title;
    // console.log(this.title)
    this.id = this.$route.query.id;
    this.getData();
  },
  methods: {
    handleCurrentChange, handleSizeChange, handleSortChange,

    getData(order) {
      this.tableLoading = true;
      order || (order = {});
      const url = 'member/CapitalDetails';
      const data = {
        uid: this.id,
        pageNumber: this.currentPage,
        pageSize: this.pageSize,
        capita_type: this.title === '资金' ? 1 : this.title === '赠送金' ? 0 : 2
      };
      order.sortName && (data.sortName = order.sortName);
      order.sortOrder && (data.sortOrder = order.sortOrder);
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        this.tableData = res.rows;
        this.total = res.total;
        this.tableLoading = false;
      }).catch(e => {
        this.tableLoading = false;
        this.$message.error(e.msg + ',请刷新或联系管理员');
      })
    },

    /**
     * 修改选择客户的赠送金及资金
     */
    showMoneyChange() {
      this.form = {
        money_type: 1,
        type: 1,
        money: 0,
        remarks: ''
      }
      this.form.id = this.id;
      this.moneyChangeDialogVisible = true;
    },
    submitMoneyChange(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const data = this.form;
          if (this.title === '资金') {
            data.c = 'upwallet';
          } else if (this.title === '赠送金') {
            data.c = 'updonationamount';
          } else if (this.title === '保证金') {
            data.c = 'assure';
          }
          const url = 'member/moneyEdit/' + this.id;
          this.$store.dispatch('GetConnect', { url, data }).then(res => {
            this.$message.success(res.msg);
            this.getData();
            this.moneyChangeDialogVisible = false;
          }).catch((err) => {
            this.$message.error(err.msg);
          })
        } else {
          this.$message.error('提交失败,请检查必填项');
        }
      })
    },

    resetMoneyChangeDialog(formName) {
      this.$refs[formName].resetFields();
    },

    toDelete() {
      this.$confirm('此操作将删除该业绩订单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      });
    }
  }
}
</script>

<style lang="scss" type="text/scss" scoped>
  @import '../../styles/index.scss';
  .box-1 {
    .model {
      .el-card {
        color: #fff;
        background-color: $panGreen;
        span {
          font-size: 14px;
        }
        div {
          font-size: 24px;
          margin-top: 10px;
        }
      }
    }
    .header {
      @include flex-row(center, space-between);
      flex-wrap: wrap;
      padding-bottom: 15px;
      div {
        @include flex-row(center, flex-start);
        flex-wrap: wrap;
        .do-btn {
          @extend .bg-green;
          margin-left: 6px;
        }
        .search-input {
          width: 240px;
        }
      }
    }
  }
</style>

